<?php

use Illuminate\Database\Seeder;

class UserInterPushInfoTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $user_inter_push_info = \App\Model\UserInterPushInfo::get();

        $article_id = \App\Model\Articles::pluck ("id")->toArray();


        foreach ($user_inter_push_info as $item){
            $user =  \App\User::whereId ($item->user_id)->first();
            if ($user){
                echo $user->id.",";
                if (isset($user->cards)){
                    $style = 4;
                    $material = $this->getUserMeterial(4,$user->cards->id);

                }else{
                    $style = 1;
                    $material = $this->getUserMeterial(1, array_rand($article_id));
                }
                if ($material){
                    $item->material_id = $material->id;
                    $item->material_type = $material->getMorphClass();
                    $item->material_style = $style;
                    $item->save();
                }else{
                    $material2 = $this->getUserMeterial(1, rand(500,600));
                    $item->material_id = $material2->id;
                    $item->material_type = $material2->getMorphClass();
                    $item->material_style = 1;
                    $item->save();
                }
            }else{
                echo "没有用户";
            }
        }
    }


    public function getUserMeterial($material_style, $material_id)
    {
        switch ($material_style){
            case 1:
                if ($material_id){
                    return \App\Model\Articles::whereId ($material_id)->first();
                }
                return \App\Model\Articles::take(1)->first();
                break;
            case 2:
                return \App\Model\PersonalPageTemplateUser::whereId($material_id)->first();
                break;
            case 3:
                return \App\Model\UserMarketing::whereId($material_id)->first();
                break;
            case 4:
                return  \App\Model\Cards::whereId($material_id)->first();
                break;
        }

    }
}
